/*
 *说明：访问控制信息表 首页脚本
 *
 *@author liuxun 2012-03-26
*/

var opeForm;
var grid;

$(function(){
	opeForm = $('#opeForm');

	grid=$('#listTable').datagrid({
		frozenColumns:[[
            {title:'选择',field:'ck',checkbox:true}
		]],
		columns:[[

            {title:'节点名称',field:'NODE_DECODE',align:'center',width:80},
            
            {title:'资源组',field:'RSC_GRP_NAME',align:'center',width:80},

            {title:'资源',field:'RSC_NAME',align:'center',width:100},
            
            {title:'账号',field:'RSC_ACC',align:'center',width:100},

            {title:'协议',field:'PRO_NAME',align:'center',width:60},
            
            {title:'端口',field:'PORT',align:'center',width:60},
            
            {title:'开始时间',field:'START_TIME',align:'center',width:120},
            
            {title:'有效期(分钟)',field:'AVAILABLE_LENGTH',align:'center',width:80,
            	formatter:function(value, rec){
            		return value == null ? '': value;
            	}
            },

            {title:'状态',field:'ACL_STATUS',align:'center',width:40,
            	formatter:function(value,rec){
            		return value == '01' ? "激活" : "<font color='red'>失效</font>";
			    }
            },
            
            {title:'策略',field:'CMD_POLICY_NAME',align:'center',width:80},
            
			{title:'操作',field:'operation',align:'center',width:100,
	        	formatter:function(value,rec){
	        		var s = '<a style="cursor:pointer; color:blue" onclick=\'showInfo("' + rec.ACL_ID + '")\'>查看明细</a>';
	        	    return s;
		   		}
		    }
        ]],
		pagination:false,
		singleSelect: false
	});

	queryOperation();

	$('#queryBtn').click(queryOperation);
	$('#clearBtn').click(clearOperation);
	$('#reloadBtn').click(refreshWin);
	$('#addBtn').click(addOperation);
	$('#editBtn').click(editOperation);
	$('#deleteBtn').click(deleteOperation);
});

//翻页时自动调用此函数
function onLoadPrev(msg){
	var queryParams=grid.datagrid('options').queryParams;
	queryParams['firstQuery']=false;
}

//查询操作
function queryOperation(){
	var params = opeForm.serializeObject();
	grid.datagrid("options").pageNumber = 1;
	grid.datagrid({
		url: 'D_ACL!queryUsrBelongUsrgrpSelectedAuthRscList.do',
		queryParams: jQuery.extend({}, params, {'firstQuery': true})
	});
}

//清空操作（重置）
function clearOperation() {
	opeForm.resetForm();
}

// 选择策略组
function showPolicGrp(key) {
	var action = "D_POLICY_GRP!showTree.do";
	var row = showModalWin(action);
	
	if (row) {
		var policy_grp_id = row.attributes.policy_grp_id;
		var policy_grp_name = row.attributes.policy_grp_name;
		
		var params = {'model.acl_id': key, 'model.policy_grp_id': policy_grp_id};
		$ .ajax({
			url: 'D_ACL!doUpdate.do',
			data: params,
			type: 'post',
			success: function(data){
			    showInfoWin('操作成功!');
				afterOK();
			}
		});
	}
	
}

//查看明细
function showInfo(key) {
	var url = 'D_ACL!showInfo.do?model.key=' + key;
	openWin(url,'pop'); 
}

//添加访问控制信息表
function addOperation(){  
	var url = 'D_ACL!showAdd.do';
	openWin(url,'pop');
}

//编辑访问控制信息表
function editOperation(){  
	var rows = grid.datagrid('getSelections');

	if (rows.length == 0) {
		showInfoWin('请您首先选中一条记录!');
		return;
	}  

	if (rows.length > 1) {
		showInfoWin('只能选择一条记录');
      	return; 
	}

	var url = 'D_ACL!showEdit.do?model.key=' + rows[0].key;
	openWin(url,'pop');
}   

//删除访问控制信息表
function deleteOperation() {
	var rows = grid.datagrid('getSelections');

	if (rows.length == 0) {
		showInfoWin('请您首先选中要删除的记录!');	
		return;
	}

	var ids = [];
	for (var i = 0; i < rows.length; i++) {
		ids.push(rows[i].key);
	}
	var params = $ .param({'id' : ids}, true);

	$ .messager.confirm('提示信息', '您确定要删除选定的' + rows.length + '条记录吗?', function(result){
		if(result){
			$ .ajax({
				url: 'D_ACL!doDelete.do',
				data: params,
				type: 'post',
				success: function(data){
				    showInfoWin('操作成功!');
					afterOK();
				}
			});
		}	
	});
}

//操作成功后刷新列表
function afterOK(){
	queryOperation();
}